var allobj = null;
//初始化所有checkbox为false
function doCheckinit(checkName){
	var gramaCkecks = document.all[checkName];
	if(gramaCkecks.length){
		for(var j=0;j<gramaCkecks.length;j++){
			gramaCkecks[j].checked = false;
		}
	}else{
		gramaCkecks.checked = false;
	}
}
//根据参数集合值与树结构的value中的key值进行比较后checked
function doCompare(gramas){
	var gramaCkecks = document.all["gramaCkeck"];
	for(var i=0;i<gramas.length;i++){
		if(gramaCkecks.length){
			for(var j=0;j<gramaCkecks.length;j++){
				if(gramas[i]==gramaCkecks[j].value){
					gramaCkecks[j].checked=true;
				}
			}
		}else{
			if(gramas[i]==gramaCkecks.value){
				gramaCkecks.checked=true;
			}
		}
	}
}
//点击角色显示对应的权限信息
function doshowGrama(obj){
	allobj = obj;
	doCheckinit("gramaCkeck");
	var dom = window.event.srcElement;
  	dom = __getReal(dom, "tagName", "SPAN");
  	var ga = dom.all.grama.value;
  	var roleids = document.all["roleid"];
  	if(typeof(roleids)!="undefined"){
		if(roleids.length){
			for(var i=0;i<roleids.length;i++){
				if(roleids[i].checked && roleids[i].previousSibling.value != obj.value){
					ga += roleids[i].previousSibling.value;
				}
			}
		}else{
			if(roleids.checked && roleids.previousSibling.value != obj.value){
				ga += roleids.previousSibling.value;
			}
		}
	}
	var gas = ga.split(',');
	doCompare(gas);
}
function doclickRole(obj){
	
}
//点击权限的checkbox 
function doclickGrama(obj){
	var gramaCkecks = document.all["gramaCkeck"];
	if(typeof(gramaCkecks)!="undefined"){
		//如果nodeName值为ALL的是全选 其他的为else
		if(obj.nodeName=="ALL"){
			if(obj.checked){
				if(gramaCkecks.length){
					for(var j=0;j<gramaCkecks.length;j++){
						gramaCkecks[j].checked=true;
					}
				}else{
					gramaCkecks.checked=true;
				}
			}else{
				if(gramaCkecks.length){
					for(var j=0;j<gramaCkecks.length;j++){
						gramaCkecks[j].checked=false;
					}
				}else{
					gramaCkecks.checked=false;
				}
			}
		//其他情况根据对应的key值进行比较选中
		}else{
			if(gramaCkecks.length){
				for(var i=0;i<gramaCkecks.length;i++){
					if(gramaCkecks[i].nodeName.indexOf("."+obj.value+".")!=-1 && obj.checked==true){
						gramaCkecks[i].checked=true;
					}else if(gramaCkecks[i].nodeName.indexOf("."+obj.value+".")!=-1 && obj.checked==false){
						gramaCkecks[i].checked=false;
					}
				}
				for(var i=0;i<gramaCkecks.length;i++){
					if(obj.nodeName.indexOf("."+gramaCkecks[i].value+".")!=-1 && obj.checked==true){
						gramaCkecks[i].checked=true;
					}
				}
			}else{
				if(gramaCkecks.nodeName.indexOf("."+obj.value+".")!=-1 && obj.checked){
					gramaCkecks.checked=true;
				}else if(gramaCkecks.nodeName.indexOf("."+obj.value+".")!=-1 && obj.checked==false){
					gramaCkecks.checked=false;
				}
				if(obj.nodeName.indexOf("."+gramaCkecks.value+".")!=-1 && obj.checked==true){
					gramaCkecks.checked=true;
				}
			}
		}
	}
}
//保存角色对应的权限 或者是 用户的权限及角色
function doSaveGR(flag){
	var map = {};
	var roleids = gainCheckValue(document,"roleid",",");
	var gramaCkecks = gainCheckValue(document,"gramaCkeck",",");
	if(roleids=="" || roleids==","){
		alert("请至少选择一个角色");
		return false;
	}
	if(gramaCkecks=="" || gramaCkecks==","){
		alert("请至少选择一个权限");
		return false;
	}
	if(confirm("确认保存信息?")){
		map["perid"] = __get("userPk").value; //人员id
		map["grama"] = gramaCkecks; //权限集合
		map["roleid"] = roleids;  //角色集合
		DWREngine.setAsync(false);
		ajax.SaveGramaRole(map,flag,function(rflag){
			__alert(rflag);
			if(flag=="R"){
				saveReback(roleids,gramaCkecks);
				doCheckinit("roleid");
				doCheckinit("gramaCkeck");
			}
			return true;
		});
	}
}
//保存权限信息到角色后调用方法将新的权限赋予到hidden里
function saveReback(roleids,gramaCkecks){
	var allcheck = document.all["roleid"];
	if(typeof(allcheck)!="undefined"){
		if(allcheck.length){
			for(var i=0;i<allcheck.length;i++){
				if(roleids.indexOf(allcheck[i].value+",")!=-1){
					allcheck[i].previousSibling.value = gramaCkecks.replace("ROOT,", "");
				}
			}
		}else{
			if(roleids.indexOf(allcheck.value+",")!=-1){
				allcheck.previousSibling.value = gramaCkecks.replace("ROOT,", "");
			}
		}
	}
}
//跳转保存角色信息的页面或者删除角色信息
function domanageRole(mark){
	if(mark=="A"){
		var dom = window.event.srcElement;
	    var fix = [];
	    fix.x = window.event.x;
	    fix.y = window.event.y;
		var p = __get("RoleAdd");
        __xopen(p, "center=true;width=250;height=120;", fix);
	}else if(mark=="D"){
		//删除角色信息
		var rid = gainCheckValue(document,"roleid",",");
		if(rid!=","){
			DWREngine.setAsync(false);
			ajax.deleteInfo("HzsysRoleinfo","roleid",rid,function(rflag){
				if(rflag){
					__alert("删除角色信息成功！");
					//遍历所有角色信息
					document.location.reload();
				}else{
					__alert("删除角色信息失败！");
				}
			});
		}else{
			__alert("请选择要删除的角色信息！");
		}
	}
}


//获取作用于下所有参数名的checkbox的已选中的value值，以参数符号进行分割的字符串
function  gainCheckValue($dom,checkName,chars){
	var checkValue = chars;
	var allcheck = $dom.all[checkName];
	if(typeof(allcheck)!="undefined"){
		if(allcheck.length){
			for(var i=0;i<allcheck.length;i++){
				if(allcheck[i].checked){
					checkValue += allcheck[i].value + chars;
				}
			}
		}else{
			if(allcheck.checked){
				checkValue += allcheck.value + chars;
			}
		}
	}
	return checkValue;
}

//按照参数的value值选中相应的checkbox
function gainValueCheck($dom,checkName,chars,cvalue){
	var allcheck = $dom.all[checkName];
	if(typeof(allcheck)!="undefined"){
		if(allcheck.length){
			for(var i=0;i<allcheck.length;i++){
				if(cvalue.indexOf(chars+allcheck[i].value+chars)!=-1){
					allcheck[i].checked=true;
				}
			}
		}else{
			if(cvalue.indexOf(chars+allcheck.value+chars)!=-1){
				allcheck.checked=true;
			}
		}
	}
}

